package com.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.train.member.resp.passenger.PassengerPageResp;
import com.train.common.enums.impl.BizExceptionEnums;
import com.train.member.enums.PassengerTypeEnums;
import com.train.common.exception.GlobalException;
import com.train.common.resp.PageResp;
import com.train.common.utils.helper.UserHelper;
import com.train.member.dto.PassengerAddOrEditDTO;
import com.train.member.dto.PassengerPageDTO;
import com.train.member.mapper.PassengerMapper;
import com.train.member.po.PassengerPO;
import com.train.member.resp.passenger.PassengerListResp;
import com.train.member.service.PassengerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author baitiaojun
* @description 针对表【passenger(乘车人)】的数据库操作Service实现
* @createDate 2025-02-14 19:37:18
*/
@Service
@RequiredArgsConstructor
public class PassengerServiceImpl extends ServiceImpl<PassengerMapper, PassengerPO>
    implements PassengerService {

    private final PassengerMapper passengerMapper;

    @Override
    public void addOrEditPassenger(PassengerAddOrEditDTO passengerAddOrEditDTO) {
        //类型校验
        if (PassengerTypeEnums.getByCode(passengerAddOrEditDTO.getType()) == null) {
            throw new GlobalException(BizExceptionEnums.EXP_PASSENGER_TYPE);
        }

        //校验id是否存在,判断是insert还是update
        if (passengerAddOrEditDTO.getId() == null) {
            //获取用户id
            Long userId = UserHelper.getUserId();
            if (userId == null) {
                throw new GlobalException(BizExceptionEnums.EXP_MEMBER_NOT_EXISTS);
            }

            //校验该会员是否已经存在该乘车人
            if (passengerMapper.existsMemberId(userId, passengerAddOrEditDTO.getIdCard())) {
                throw new GlobalException(BizExceptionEnums.EXP_PASSENGER_EXISTS);
            }

            //添加乘客id
            passengerAddOrEditDTO.setMemberId(userId);
        } else {
            if (!passengerMapper.existsId(passengerAddOrEditDTO.getId())) {
                throw new GlobalException(BizExceptionEnums.EXP_PASSENGER_NOT_EXISTS);
            }
        }

        PassengerPO passengerPO = BeanUtil.copyProperties(passengerAddOrEditDTO, PassengerPO.class);
//        boolean isAdd = passengerMapper.addPassenger(passengerPO);
        boolean isSaved = this.saveOrUpdate(passengerPO);
        if (!isSaved) {
            throw new GlobalException(BizExceptionEnums.EXP_ADD_PASSENGER);
        }
    }

    @Override
    public PageResp<PassengerPageResp> pageList(PassengerPageDTO passengerPageDTO) {
        return passengerMapper.pageList(passengerPageDTO);
    }

    @Override
    public void remove(Long id) {
        boolean isRemoved = this.removeById(id);
        if (!isRemoved) {
            throw new GlobalException(BizExceptionEnums.EXP_REMOVE_PASSENGER);
        }
    }

    @Override
    public List<PassengerListResp> queryPassengerList() {
        Long userId = UserHelper.getUserId();
        if (userId == null) {
            throw new GlobalException(BizExceptionEnums.EXP_MEMBER_NOT_EXISTS);
        }
        return passengerMapper.selectPassengerList(userId);
    }
}




